<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<meta name="generator" content="pdoc 0.9.2" />
<title>ascend.video.video_writer API documentation</title>
<meta name="description" content="Copyright 2020 Huawei Technologies Co., Ltd
Licensed under the Apache License, Version 2.0 (the &#34;License&#34;);
you may not use this file except in …" />
<link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/sanitize.min.css" integrity="sha256-PK9q560IAAa6WVRRh76LtCaI8pjTJ2z11v0miyNNjrs=" crossorigin>
<link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/typography.min.css" integrity="sha256-7l/o7C8jubJiy74VsKTidCy1yBkRtiUGbVkYBylBqUg=" crossorigin>
<link rel="stylesheet preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/github.min.css" crossorigin>
<style>:root{--highlight-color:#fe9}.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:30px;overflow:hidden}#sidebar > *:last-child{margin-bottom:2cm}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:1em 0 .50em 0}h3{font-size:1.4em;margin:25px 0 10px 0}h4{margin:0;font-size:105%}h1:target,h2:target,h3:target,h4:target,h5:target,h6:target{background:var(--highlight-color);padding:.2em 0}a{color:#058;text-decoration:none;transition:color .3s ease-in-out}a:hover{color:#e82}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900}pre code{background:#f8f8f8;font-size:.8em;line-height:1.4em}code{background:#f2f2f1;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{background:#f8f8f8;border:0;border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0;padding:1ex}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{margin-top:.6em;font-weight:bold}@media (min-width:200ex){#index .two-column{column-count:2}}@media (min-width:300ex){#index .two-column{column-count:3}}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-weight:bold;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}dt:target .name{background:var(--highlight-color)}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source summary,.git-link-div{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase}.source summary > *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:1em}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{font-size:12px;overflow:visible}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}td{padding:0 .5em}.admonition{padding:.1em .5em;margin-bottom:1em}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style>
<style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%;height:100vh;overflow:auto;position:sticky;top:0}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.item .name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul{padding-left:1.5em}.toc > ul > li{margin-top:.5em}}</style>
<style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style>
<link rel="preconnect" href="https://www.google.com">
<script async src="https://cse.google.com/cse.js?cx=017837193012385208679:pey8ky8gdqw"></script>
<style>
.gsc-control-cse {padding:0 !important;margin-top:1em}
body.gsc-overflow-hidden #sidebar {overflow: visible;}
</style>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js" integrity="sha256-Uv3H6lx7dJmRfRvH8TH6kJD1TSK1aFcwgx+mdg3epi8=" crossorigin></script>
<script>window.addEventListener('DOMContentLoaded', () => hljs.initHighlighting())</script>
<style>.homelink{display:block;font-size:2em;font-weight:bold;color:#555;padding-bottom:.5em;border-bottom:1px solid silver}.homelink:hover{color:inherit}.homelink img{max-width:20%;max-height:5em;margin:auto;margin-bottom:.3em}</style>
<link rel="canonical" href="https://bazige.github.io/ascend/video/video_writer.html">
<link rel="icon" href="https://bazige.github.io/ascend/logo/logo.png"">
</head>
<body>
<main>
<article id="content">
<header>
<h1 class="title">Module <code>ascend.video.video_writer</code></h1>
</header>
<section id="section-intro">
<p>Copyright 2020 Huawei Technologies Co., Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
<a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.</p>
<details class="source">
<summary>
<span>Expand source code</span>
<a href="https://github.com/pdoc3/pdoc/blob/37c907488ff52b174ab602690d1166f28bded9a2/ascend/video/video_writer.py#L0-L238" class="git-link">Browse git</a>
</summary>
<pre><code class="python">#!/usr/bin/env python3
# -*- coding: utf-8 -*-
&#34;&#34;&#34;
Copyright 2020 Huawei Technologies Co., Ltd
Licensed under the Apache License, Version 2.0 (the &#34;License&#34;);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an &#34;AS IS&#34; BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
&#34;&#34;&#34;

from enum import Enum
from ..common.log import Log
from ..common.const import *
from ..common.path import is_filepath
from ..data.ascendarray import AscendArray
from .venc import Venc
from .frame import Frame


class Status(Enum):
    &#34;&#34;&#34;An enum that defines decode work status.

    Contains `IDLE`, `READY`, `START`, `RUN`, `END`.
    &#34;&#34;&#34;
    IDLE = 0
    READY = 1
    RUN = 2
    END = 3

class VideoWriter():
    &#34;&#34;&#34;Define a VideoWriter class to encode video stream with format h264/265.

    Attributes:
        encode (Venc obj): The DVPP video encoder object.
        fps (int): frame per second
        frameSize (tuple): frame shape to be encoded, like (w,h).
        width (int): Width of the frames in the video stream
        height (int): Height of the frames in the video stream.
        frames (int): Number of frames in the video file.
        format (int): video stream format
        fps (float): Frame rate.
        bitrate (int): (read-only) Video bitrate in kbits/s
        pix_fmt (int): the encode frame&#39;s pixel format, only support yuv420 NV21/12

    Methods:
        - write   : Writes the next video frame
        - set     : Sets a property in the VideoWriter
        - get     : Returns the specified VideoWriter property
        - release : Closes and release video writer&#39;s resource

    &#34;&#34;&#34;
    def __init__(self, context, filename, fps, frameSize, is_color=False):
        if not isinstance(context, int):
            raise TypeError(f&#34;VideoWriter input context expects an int, bug got {type(context)}.&#34;)

        if not is_filepath(filename):
            raise TypeError(f&#34;VideoWriter input filename expects an str or Path, bug got {type(filename)}.&#34;)

        if not isinstance(fps, int):
            raise TypeError(f&#34;VideoWriter input fps expects an int, bug got {type(fps)}.&#34;)

        if not isinstance(frameSize, tuple) or len(frameSize) != 2:
            raise TypeError(f&#34;VideoWriter input frameSize expects a 2-elements tuple, bug got {type(fps)}.&#34;)

        self._context = context

        # check input param ok
        self._status = Status.IDLE

        # initial encoder
        self.encode = Venc(context, filename, frameSize[0], frameSize[1])

        # configure fps
        self.encode.fps = fps

        # intial ok, and step to READY status
        self._status = Status.READY

    @property
    def width(self):
        return self.encode.width

    @width.setter
    def width(self, width):
        if self._status == Status.READY:
            self.encode.width = width
            return True
        else:
            return False

    @property
    def height(self):
        return self.encode.height

    @height.setter
    def height(self, height):
        if self._status == Status.READY:
            self.encode.height = height
            return True
        else:
            return False

    @property
    def fps(self):
        return self.encode.fps

    @fps.setter
    def fps(self, fps):
        if self._status == Status.READY:
            self.encode.fps = fps
            return True
        else:
            return False

    @property
    def pix_fmt(self):
        return self.encode.pic_format

    @pix_fmt.setter
    def pix_fmt(self, pix_fmt):
        if self._status == Status.READY:
            self.encode.pic_format = pix_fmt
            return True
        else:
            return False


    def write(self, image):
        &#34;&#34;&#34;The function/method writes the specified image to video file. It must have the same size as has
            been specified when opening the video writer.
        Args:
            image (AscendArray): The written frame. In general, color images are expected in BGR format.

        &#34;&#34;&#34;
        if self._status in [Status.READY, Status.RUN] and image:
            if not isinstance(image, AscendArray):
                self._status = Status.END
                raise TypeError(f&#34;Input image expects an AscendArray, but got {type(image)}.&#34;)

            frame = Frame(image)
            self.encode.process(frame)
            self._status = Status.RUN
            Log(INFO, &#39;write one frame to VideoWriter.&#39;)

        else:
            Log(WARNING, f&#39;write image in status {self._status}.&#39;)

    def set(self, attr, value):
        &#34;&#34;&#34; set(attr, value) -&gt; retval
            Sets a property in the VideoWriter
        Args:
            attr (str): Property from VideoWriter Properties (eg. &#39;width&#39;, &#39;fps&#39;, ...)
            value (int): Value of the property

        Returns:
            bool : `True` if the property is supported by the backend used by the VideoWriter instance.
        &#34;&#34;&#34;
        if self._status == Status.READY:
            if attr == &#39;width&#39;: 
                self.encode.width = value
            elif attr == &#39;height&#39;: 
                self.encode.height = value
            elif attr == &#39;fps&#39;: 
                self.encode.fps = value
            elif attr == &#39;enc_type&#39;: 
                self.encode.encoder_type = value
            elif attr == &#39;pix_fmt&#39;: 
                self.encode.pic_format = value
            elif attr == &#39;bit_rate&#39;: 
                self.encode.bit_rate = value
            elif attr == &#39;key_frame&#39;: 
                self.encode.frame_itval = value
            elif attr == &#39;rc_mode&#39;: 
                self.encode.rc_mode = value
            else:
                Log(ERROR, f&#39;attr {attr} is not support in VideoWriter.&#39;)
                return False
            return True
        else:
            Log(ERROR, f&#39;Set attr {attr} in status {self._status}.&#39;)
            raise ValueError(f&#34;Set attr {attr} in status {self._status}.&#34;)

    def get(self, attr):
        &#34;&#34;&#34; get(attr) -&gt; retval
            Returns the specified VideoWriter property
        Args:
            attr (str): Property from VideoWriter Properties (eg. &#39;width&#39;, &#39;fps&#39;, ...)
            
        Returns:
            [int]: Value for the specified property. Value 0 is returned when querying a 
                property that is not supported by the backend used by the VideoWriter instance.
        &#34;&#34;&#34;
        if not isinstance(attr, str):
            raise TypeError(f&#34;Input attr expects a string, but got {type(attr)}.&#34;)
        
        attr_dict = {
            &#39;width&#39;       : self.encode.width,
            &#39;height&#39;      : self.encode.height,
            &#39;fps&#39;         : self.encode.fps,
            &#39;enc_type&#39;    : self.encode.encoder_type,
            &#39;pix_fmt&#39;     : self.encode.pic_format,
            &#39;bit_rate&#39;    : self.encode.bit_rate,
            &#39;key_frame&#39;   : self.encode.frame_itval,
            &#39;rc_mode&#39;     : self.encode.rc_mode
        }

        try:
            return attr_dict[attr]
        except KeyError:
            return 

    def release(self):
        &#34;&#34;&#34;Closes video file or writer and release resource.
        &#34;&#34;&#34;
        if self._status == Status.RUN:
            frame = Frame(None, is_last=True)
            self.encode.process(frame)
            self._status = Status.END

            # do finish job and stop encoder thread
            self.encode.finish()

            Log(INFO, f&#34;write last frame to VideoWriter.&#34;)

        if hasattr(self, &#39;encode&#39;):
            del self.encode
        
        self._status = Status.END

    def __del__(self):
        if hasattr(self, &#39;encode&#39;):
            del self.encode
        </code></pre>
</details>
</section>
<section>
</section>
<section>
</section>
<section>
</section>
<section>
<h2 class="section-title" id="header-classes">Classes</h2>
<dl>
<dt id="ascend.video.video_writer.Status"><code class="flex name class">
<span>class <span class="ident">Status</span></span>
<span>(</span><span>value, names=None, *, module=None, qualname=None, type=None, start=1)</span>
</code></dt>
<dd>
<div class="desc"><p>An enum that defines decode work status.</p>
<p>Contains <code>IDLE</code>, <code>READY</code>, <code>START</code>, <code>RUN</code>, <code>END</code>.</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
<a href="https://github.com/pdoc3/pdoc/blob/37c907488ff52b174ab602690d1166f28bded9a2/ascend/video/video_writer.py#L25-L33" class="git-link">Browse git</a>
</summary>
<pre><code class="python">class Status(Enum):
    &#34;&#34;&#34;An enum that defines decode work status.

    Contains `IDLE`, `READY`, `START`, `RUN`, `END`.
    &#34;&#34;&#34;
    IDLE = 0
    READY = 1
    RUN = 2
    END = 3</code></pre>
</details>
<h3>Ancestors</h3>
<ul class="hlist">
<li>enum.Enum</li>
</ul>
<h3>Class variables</h3>
<dl>
<dt id="ascend.video.video_writer.Status.END"><code class="name">var <span class="ident">END</span></code></dt>
<dd>
<div class="desc"></div>
</dd>
<dt id="ascend.video.video_writer.Status.IDLE"><code class="name">var <span class="ident">IDLE</span></code></dt>
<dd>
<div class="desc"></div>
</dd>
<dt id="ascend.video.video_writer.Status.READY"><code class="name">var <span class="ident">READY</span></code></dt>
<dd>
<div class="desc"></div>
</dd>
<dt id="ascend.video.video_writer.Status.RUN"><code class="name">var <span class="ident">RUN</span></code></dt>
<dd>
<div class="desc"></div>
</dd>
</dl>
</dd>
<dt id="ascend.video.video_writer.VideoWriter"><code class="flex name class">
<span>class <span class="ident">VideoWriter</span></span>
<span>(</span><span>context, filename, fps, frameSize, is_color=False)</span>
</code></dt>
<dd>
<div class="desc"><p>Define a VideoWriter class to encode video stream with format h264/265.</p>
<h2 id="attributes">Attributes</h2>
<dl>
<dt><strong><code>encode</code></strong> :&ensp;<code>Venc obj</code></dt>
<dd>The DVPP video encoder object.</dd>
<dt><strong><code>fps</code></strong> :&ensp;<code>int</code></dt>
<dd>frame per second</dd>
<dt><strong><code>frameSize</code></strong> :&ensp;<code>tuple</code></dt>
<dd>frame shape to be encoded, like (w,h).</dd>
<dt><strong><code>width</code></strong> :&ensp;<code>int</code></dt>
<dd>Width of the frames in the video stream</dd>
<dt><strong><code>height</code></strong> :&ensp;<code>int</code></dt>
<dd>Height of the frames in the video stream.</dd>
<dt><strong><code>frames</code></strong> :&ensp;<code>int</code></dt>
<dd>Number of frames in the video file.</dd>
<dt><strong><code>format</code></strong> :&ensp;<code>int</code></dt>
<dd>video stream format</dd>
<dt><strong><code>fps</code></strong> :&ensp;<code>float</code></dt>
<dd>Frame rate.</dd>
<dt><strong><code>bitrate</code></strong> :&ensp;<code>int</code></dt>
<dd>(read-only) Video bitrate in kbits/s</dd>
<dt><strong><code>pix_fmt</code></strong> :&ensp;<code>int</code></dt>
<dd>the encode frame's pixel format, only support yuv420 NV21/12</dd>
</dl>
<h2 id="methods">Methods</h2>
<ul>
<li>write
: Writes the next video frame</li>
<li>set
: Sets a property in the VideoWriter</li>
<li>get
: Returns the specified VideoWriter property</li>
<li>release : Closes and release video writer's resource</li>
</ul></div>
<details class="source">
<summary>
<span>Expand source code</span>
<a href="https://github.com/pdoc3/pdoc/blob/37c907488ff52b174ab602690d1166f28bded9a2/ascend/video/video_writer.py#L35-L237" class="git-link">Browse git</a>
</summary>
<pre><code class="python">class VideoWriter():
    &#34;&#34;&#34;Define a VideoWriter class to encode video stream with format h264/265.

    Attributes:
        encode (Venc obj): The DVPP video encoder object.
        fps (int): frame per second
        frameSize (tuple): frame shape to be encoded, like (w,h).
        width (int): Width of the frames in the video stream
        height (int): Height of the frames in the video stream.
        frames (int): Number of frames in the video file.
        format (int): video stream format
        fps (float): Frame rate.
        bitrate (int): (read-only) Video bitrate in kbits/s
        pix_fmt (int): the encode frame&#39;s pixel format, only support yuv420 NV21/12

    Methods:
        - write   : Writes the next video frame
        - set     : Sets a property in the VideoWriter
        - get     : Returns the specified VideoWriter property
        - release : Closes and release video writer&#39;s resource

    &#34;&#34;&#34;
    def __init__(self, context, filename, fps, frameSize, is_color=False):
        if not isinstance(context, int):
            raise TypeError(f&#34;VideoWriter input context expects an int, bug got {type(context)}.&#34;)

        if not is_filepath(filename):
            raise TypeError(f&#34;VideoWriter input filename expects an str or Path, bug got {type(filename)}.&#34;)

        if not isinstance(fps, int):
            raise TypeError(f&#34;VideoWriter input fps expects an int, bug got {type(fps)}.&#34;)

        if not isinstance(frameSize, tuple) or len(frameSize) != 2:
            raise TypeError(f&#34;VideoWriter input frameSize expects a 2-elements tuple, bug got {type(fps)}.&#34;)

        self._context = context

        # check input param ok
        self._status = Status.IDLE

        # initial encoder
        self.encode = Venc(context, filename, frameSize[0], frameSize[1])

        # configure fps
        self.encode.fps = fps

        # intial ok, and step to READY status
        self._status = Status.READY

    @property
    def width(self):
        return self.encode.width

    @width.setter
    def width(self, width):
        if self._status == Status.READY:
            self.encode.width = width
            return True
        else:
            return False

    @property
    def height(self):
        return self.encode.height

    @height.setter
    def height(self, height):
        if self._status == Status.READY:
            self.encode.height = height
            return True
        else:
            return False

    @property
    def fps(self):
        return self.encode.fps

    @fps.setter
    def fps(self, fps):
        if self._status == Status.READY:
            self.encode.fps = fps
            return True
        else:
            return False

    @property
    def pix_fmt(self):
        return self.encode.pic_format

    @pix_fmt.setter
    def pix_fmt(self, pix_fmt):
        if self._status == Status.READY:
            self.encode.pic_format = pix_fmt
            return True
        else:
            return False


    def write(self, image):
        &#34;&#34;&#34;The function/method writes the specified image to video file. It must have the same size as has
            been specified when opening the video writer.
        Args:
            image (AscendArray): The written frame. In general, color images are expected in BGR format.

        &#34;&#34;&#34;
        if self._status in [Status.READY, Status.RUN] and image:
            if not isinstance(image, AscendArray):
                self._status = Status.END
                raise TypeError(f&#34;Input image expects an AscendArray, but got {type(image)}.&#34;)

            frame = Frame(image)
            self.encode.process(frame)
            self._status = Status.RUN
            Log(INFO, &#39;write one frame to VideoWriter.&#39;)

        else:
            Log(WARNING, f&#39;write image in status {self._status}.&#39;)

    def set(self, attr, value):
        &#34;&#34;&#34; set(attr, value) -&gt; retval
            Sets a property in the VideoWriter
        Args:
            attr (str): Property from VideoWriter Properties (eg. &#39;width&#39;, &#39;fps&#39;, ...)
            value (int): Value of the property

        Returns:
            bool : `True` if the property is supported by the backend used by the VideoWriter instance.
        &#34;&#34;&#34;
        if self._status == Status.READY:
            if attr == &#39;width&#39;: 
                self.encode.width = value
            elif attr == &#39;height&#39;: 
                self.encode.height = value
            elif attr == &#39;fps&#39;: 
                self.encode.fps = value
            elif attr == &#39;enc_type&#39;: 
                self.encode.encoder_type = value
            elif attr == &#39;pix_fmt&#39;: 
                self.encode.pic_format = value
            elif attr == &#39;bit_rate&#39;: 
                self.encode.bit_rate = value
            elif attr == &#39;key_frame&#39;: 
                self.encode.frame_itval = value
            elif attr == &#39;rc_mode&#39;: 
                self.encode.rc_mode = value
            else:
                Log(ERROR, f&#39;attr {attr} is not support in VideoWriter.&#39;)
                return False
            return True
        else:
            Log(ERROR, f&#39;Set attr {attr} in status {self._status}.&#39;)
            raise ValueError(f&#34;Set attr {attr} in status {self._status}.&#34;)

    def get(self, attr):
        &#34;&#34;&#34; get(attr) -&gt; retval
            Returns the specified VideoWriter property
        Args:
            attr (str): Property from VideoWriter Properties (eg. &#39;width&#39;, &#39;fps&#39;, ...)
            
        Returns:
            [int]: Value for the specified property. Value 0 is returned when querying a 
                property that is not supported by the backend used by the VideoWriter instance.
        &#34;&#34;&#34;
        if not isinstance(attr, str):
            raise TypeError(f&#34;Input attr expects a string, but got {type(attr)}.&#34;)
        
        attr_dict = {
            &#39;width&#39;       : self.encode.width,
            &#39;height&#39;      : self.encode.height,
            &#39;fps&#39;         : self.encode.fps,
            &#39;enc_type&#39;    : self.encode.encoder_type,
            &#39;pix_fmt&#39;     : self.encode.pic_format,
            &#39;bit_rate&#39;    : self.encode.bit_rate,
            &#39;key_frame&#39;   : self.encode.frame_itval,
            &#39;rc_mode&#39;     : self.encode.rc_mode
        }

        try:
            return attr_dict[attr]
        except KeyError:
            return 

    def release(self):
        &#34;&#34;&#34;Closes video file or writer and release resource.
        &#34;&#34;&#34;
        if self._status == Status.RUN:
            frame = Frame(None, is_last=True)
            self.encode.process(frame)
            self._status = Status.END

            # do finish job and stop encoder thread
            self.encode.finish()

            Log(INFO, f&#34;write last frame to VideoWriter.&#34;)

        if hasattr(self, &#39;encode&#39;):
            del self.encode
        
        self._status = Status.END

    def __del__(self):
        if hasattr(self, &#39;encode&#39;):
            del self.encode</code></pre>
</details>
<h3>Instance variables</h3>
<dl>
<dt id="ascend.video.video_writer.VideoWriter.fps"><code class="name">var <span class="ident">fps</span></code></dt>
<dd>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
<a href="https://github.com/pdoc3/pdoc/blob/37c907488ff52b174ab602690d1166f28bded9a2/ascend/video/video_writer.py#L108-L110" class="git-link">Browse git</a>
</summary>
<pre><code class="python">@property
def fps(self):
    return self.encode.fps</code></pre>
</details>
</dd>
<dt id="ascend.video.video_writer.VideoWriter.height"><code class="name">var <span class="ident">height</span></code></dt>
<dd>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
<a href="https://github.com/pdoc3/pdoc/blob/37c907488ff52b174ab602690d1166f28bded9a2/ascend/video/video_writer.py#L96-L98" class="git-link">Browse git</a>
</summary>
<pre><code class="python">@property
def height(self):
    return self.encode.height</code></pre>
</details>
</dd>
<dt id="ascend.video.video_writer.VideoWriter.pix_fmt"><code class="name">var <span class="ident">pix_fmt</span></code></dt>
<dd>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
<a href="https://github.com/pdoc3/pdoc/blob/37c907488ff52b174ab602690d1166f28bded9a2/ascend/video/video_writer.py#L120-L122" class="git-link">Browse git</a>
</summary>
<pre><code class="python">@property
def pix_fmt(self):
    return self.encode.pic_format</code></pre>
</details>
</dd>
<dt id="ascend.video.video_writer.VideoWriter.width"><code class="name">var <span class="ident">width</span></code></dt>
<dd>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
<a href="https://github.com/pdoc3/pdoc/blob/37c907488ff52b174ab602690d1166f28bded9a2/ascend/video/video_writer.py#L84-L86" class="git-link">Browse git</a>
</summary>
<pre><code class="python">@property
def width(self):
    return self.encode.width</code></pre>
</details>
</dd>
</dl>
<h3>Methods</h3>
<dl>
<dt id="ascend.video.video_writer.VideoWriter.get"><code class="name flex">
<span>def <span class="ident">get</span></span>(<span>self, attr)</span>
</code></dt>
<dd>
<div class="desc"><p>get(attr) -&gt; retval
Returns the specified VideoWriter property</p>
<h2 id="args">Args</h2>
<dl>
<dt><strong><code>attr</code></strong> :&ensp;<code>str</code></dt>
<dd>Property from VideoWriter Properties (eg. 'width', 'fps', &hellip;)</dd>
</dl>
<h2 id="returns">Returns</h2>
<dl>
<dt><code>[int]</code></dt>
<dd>Value for the specified property. Value 0 is returned when querying a
property that is not supported by the backend used by the VideoWriter instance.</dd>
</dl></div>
<details class="source">
<summary>
<span>Expand source code</span>
<a href="https://github.com/pdoc3/pdoc/blob/37c907488ff52b174ab602690d1166f28bded9a2/ascend/video/video_writer.py#L188-L215" class="git-link">Browse git</a>
</summary>
<pre><code class="python">def get(self, attr):
    &#34;&#34;&#34; get(attr) -&gt; retval
        Returns the specified VideoWriter property
    Args:
        attr (str): Property from VideoWriter Properties (eg. &#39;width&#39;, &#39;fps&#39;, ...)
        
    Returns:
        [int]: Value for the specified property. Value 0 is returned when querying a 
            property that is not supported by the backend used by the VideoWriter instance.
    &#34;&#34;&#34;
    if not isinstance(attr, str):
        raise TypeError(f&#34;Input attr expects a string, but got {type(attr)}.&#34;)
    
    attr_dict = {
        &#39;width&#39;       : self.encode.width,
        &#39;height&#39;      : self.encode.height,
        &#39;fps&#39;         : self.encode.fps,
        &#39;enc_type&#39;    : self.encode.encoder_type,
        &#39;pix_fmt&#39;     : self.encode.pic_format,
        &#39;bit_rate&#39;    : self.encode.bit_rate,
        &#39;key_frame&#39;   : self.encode.frame_itval,
        &#39;rc_mode&#39;     : self.encode.rc_mode
    }

    try:
        return attr_dict[attr]
    except KeyError:
        return </code></pre>
</details>
</dd>
<dt id="ascend.video.video_writer.VideoWriter.release"><code class="name flex">
<span>def <span class="ident">release</span></span>(<span>self)</span>
</code></dt>
<dd>
<div class="desc"><p>Closes video file or writer and release resource.</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
<a href="https://github.com/pdoc3/pdoc/blob/37c907488ff52b174ab602690d1166f28bded9a2/ascend/video/video_writer.py#L217-L233" class="git-link">Browse git</a>
</summary>
<pre><code class="python">def release(self):
    &#34;&#34;&#34;Closes video file or writer and release resource.
    &#34;&#34;&#34;
    if self._status == Status.RUN:
        frame = Frame(None, is_last=True)
        self.encode.process(frame)
        self._status = Status.END

        # do finish job and stop encoder thread
        self.encode.finish()

        Log(INFO, f&#34;write last frame to VideoWriter.&#34;)

    if hasattr(self, &#39;encode&#39;):
        del self.encode
    
    self._status = Status.END</code></pre>
</details>
</dd>
<dt id="ascend.video.video_writer.VideoWriter.set"><code class="name flex">
<span>def <span class="ident">set</span></span>(<span>self, attr, value)</span>
</code></dt>
<dd>
<div class="desc"><p>set(attr, value) -&gt; retval
Sets a property in the VideoWriter</p>
<h2 id="args">Args</h2>
<dl>
<dt><strong><code>attr</code></strong> :&ensp;<code>str</code></dt>
<dd>Property from VideoWriter Properties (eg. 'width', 'fps', &hellip;)</dd>
<dt><strong><code>value</code></strong> :&ensp;<code>int</code></dt>
<dd>Value of the property</dd>
</dl>
<h2 id="returns">Returns</h2>
<dl>
<dt><code>bool </code></dt>
<dd><code>True</code> if the property is supported by the backend used by the VideoWriter instance.</dd>
</dl></div>
<details class="source">
<summary>
<span>Expand source code</span>
<a href="https://github.com/pdoc3/pdoc/blob/37c907488ff52b174ab602690d1166f28bded9a2/ascend/video/video_writer.py#L153-L186" class="git-link">Browse git</a>
</summary>
<pre><code class="python">def set(self, attr, value):
    &#34;&#34;&#34; set(attr, value) -&gt; retval
        Sets a property in the VideoWriter
    Args:
        attr (str): Property from VideoWriter Properties (eg. &#39;width&#39;, &#39;fps&#39;, ...)
        value (int): Value of the property

    Returns:
        bool : `True` if the property is supported by the backend used by the VideoWriter instance.
    &#34;&#34;&#34;
    if self._status == Status.READY:
        if attr == &#39;width&#39;: 
            self.encode.width = value
        elif attr == &#39;height&#39;: 
            self.encode.height = value
        elif attr == &#39;fps&#39;: 
            self.encode.fps = value
        elif attr == &#39;enc_type&#39;: 
            self.encode.encoder_type = value
        elif attr == &#39;pix_fmt&#39;: 
            self.encode.pic_format = value
        elif attr == &#39;bit_rate&#39;: 
            self.encode.bit_rate = value
        elif attr == &#39;key_frame&#39;: 
            self.encode.frame_itval = value
        elif attr == &#39;rc_mode&#39;: 
            self.encode.rc_mode = value
        else:
            Log(ERROR, f&#39;attr {attr} is not support in VideoWriter.&#39;)
            return False
        return True
    else:
        Log(ERROR, f&#39;Set attr {attr} in status {self._status}.&#39;)
        raise ValueError(f&#34;Set attr {attr} in status {self._status}.&#34;)</code></pre>
</details>
</dd>
<dt id="ascend.video.video_writer.VideoWriter.write"><code class="name flex">
<span>def <span class="ident">write</span></span>(<span>self, image)</span>
</code></dt>
<dd>
<div class="desc"><p>The function/method writes the specified image to video file. It must have the same size as has
been specified when opening the video writer.</p>
<h2 id="args">Args</h2>
<dl>
<dt><strong><code>image</code></strong> :&ensp;<code>AscendArray</code></dt>
<dd>The written frame. In general, color images are expected in BGR format.</dd>
</dl></div>
<details class="source">
<summary>
<span>Expand source code</span>
<a href="https://github.com/pdoc3/pdoc/blob/37c907488ff52b174ab602690d1166f28bded9a2/ascend/video/video_writer.py#L133-L151" class="git-link">Browse git</a>
</summary>
<pre><code class="python">def write(self, image):
    &#34;&#34;&#34;The function/method writes the specified image to video file. It must have the same size as has
        been specified when opening the video writer.
    Args:
        image (AscendArray): The written frame. In general, color images are expected in BGR format.

    &#34;&#34;&#34;
    if self._status in [Status.READY, Status.RUN] and image:
        if not isinstance(image, AscendArray):
            self._status = Status.END
            raise TypeError(f&#34;Input image expects an AscendArray, but got {type(image)}.&#34;)

        frame = Frame(image)
        self.encode.process(frame)
        self._status = Status.RUN
        Log(INFO, &#39;write one frame to VideoWriter.&#39;)

    else:
        Log(WARNING, f&#39;write image in status {self._status}.&#39;)</code></pre>
</details>
</dd>
</dl>
</dd>
</dl>
</section>
</article>
<nav id="sidebar">
<header>
<a class="homelink" rel="home" title="ascendfly Home" href="https://gitee.com/ascend-fae/ascendfly">
<img src="https://bazige.github.io/ascend/logo/logo.png" alt=""> ascendfly
</a>
</header>
<div class="gcse-search" style="height: 70px"
data-as_oq="site:bazige.github.io inurl:github.com/ascendfly"
data-gaCategoryParameter="ascend.video.video_writer">
</div>
<h1>Index</h1>
<div class="toc">
<ul></ul>
</div>
<ul id="index">
<li><h3>Super-module</h3>
<ul>
<li><code><a title="ascend.video" href="index.html">ascend.video</a></code></li>
</ul>
</li>
<li><h3><a href="#header-classes">Classes</a></h3>
<ul>
<li>
<h4><code><a title="ascend.video.video_writer.Status" href="#ascend.video.video_writer.Status">Status</a></code></h4>
<ul class="">
<li><code><a title="ascend.video.video_writer.Status.END" href="#ascend.video.video_writer.Status.END">END</a></code></li>
<li><code><a title="ascend.video.video_writer.Status.IDLE" href="#ascend.video.video_writer.Status.IDLE">IDLE</a></code></li>
<li><code><a title="ascend.video.video_writer.Status.READY" href="#ascend.video.video_writer.Status.READY">READY</a></code></li>
<li><code><a title="ascend.video.video_writer.Status.RUN" href="#ascend.video.video_writer.Status.RUN">RUN</a></code></li>
</ul>
</li>
<li>
<h4><code><a title="ascend.video.video_writer.VideoWriter" href="#ascend.video.video_writer.VideoWriter">VideoWriter</a></code></h4>
<ul class="two-column">
<li><code><a title="ascend.video.video_writer.VideoWriter.fps" href="#ascend.video.video_writer.VideoWriter.fps">fps</a></code></li>
<li><code><a title="ascend.video.video_writer.VideoWriter.get" href="#ascend.video.video_writer.VideoWriter.get">get</a></code></li>
<li><code><a title="ascend.video.video_writer.VideoWriter.height" href="#ascend.video.video_writer.VideoWriter.height">height</a></code></li>
<li><code><a title="ascend.video.video_writer.VideoWriter.pix_fmt" href="#ascend.video.video_writer.VideoWriter.pix_fmt">pix_fmt</a></code></li>
<li><code><a title="ascend.video.video_writer.VideoWriter.release" href="#ascend.video.video_writer.VideoWriter.release">release</a></code></li>
<li><code><a title="ascend.video.video_writer.VideoWriter.set" href="#ascend.video.video_writer.VideoWriter.set">set</a></code></li>
<li><code><a title="ascend.video.video_writer.VideoWriter.width" href="#ascend.video.video_writer.VideoWriter.width">width</a></code></li>
<li><code><a title="ascend.video.video_writer.VideoWriter.write" href="#ascend.video.video_writer.VideoWriter.write">write</a></code></li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>
</main>
<footer id="footer">
<p><span style="color:#ddd">&#21328;</span></p>
<p>Generated by <a href="https://pdoc3.github.io/pdoc"><cite>pdoc</cite> 0.9.2</a>.</p>
</footer>
</body>
</html>